package org.dragonnova.meetingserver.utils;

import java.security.MessageDigest;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.format.Time;

public class MyUtil {

    public MyUtil() {
    }

    /**
     * 按键提示音，如果为true，无论系统有没有，都将开启
     * 系统设置中若关闭了选择操作音(选取控件时播放音效)，则android:soundEffectsEnabled属性无意义，均不发声；
     * 而如果开启了设置中的选择操作音，则即使没有设置view的该属性，view.playSoundEffect(soundEffectConstants)依然会有单击提示音，
     * 除非设置该属性为false时，view才不会出现按键提示音。
     *
     * @param context 上下文
     * @param flag    是否需要按键提示音
     */
    public static void keytoneSetting(Context context, int flag) {
        Settings.System.putInt(context.getContentResolver(),
                Settings.System.SOUND_EFFECTS_ENABLED, flag);
        AudioManager mAudioManager = (AudioManager) context
                .getSystemService(Context.AUDIO_SERVICE);
        mAudioManager.loadSoundEffects();
    }


    /**
     * 获取系统时间
     *
     * @return String
     */
    public static String getSystemTime() {
        Time time = new Time();
        time.setToNow();
        int hour = time.hour;
        int minute = time.minute;
        int second = time.second;
        String hourStr = Integer.toString(hour);
        String minuteStr = Integer.toString(minute);
        String secondStr = Integer.toString(second);
        if (hour < 10) {
            hourStr = "0" + hourStr;
        }
        if (minute < 10) {
            minuteStr = "0" + minuteStr;
        }
        if (second < 10) {
            secondStr = "0" + secondStr;
        }
        String timeStr = hourStr + " : " + minuteStr + " : " + secondStr;
        return timeStr;
    }

    /**
     * 获取版本
     *
     * @param context 上下文
     * @return String
     */
    public static String getVersion(Context context) {
        String version;
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(),
                    0);
            version = info.versionName;
            return version;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            return e.toString();
        }
    }

    /**
     * ��ȡ���Ĵ��﷭����׸�Ӣ�ĵ���
     *
     * @param string String
     * @return String
     */
    public static String getEnWord(String string) {
        String str = string;
        int index = string.indexOf(";");
        if (-1 != index) {
            str = string.substring(0, index);
        }
        return str;
    }

    /**
     * ��ȡӢ�ĵ��ʷ�����׸����Ĵ���
     *
     * @param string String
     * @return String
     */
    public static String getChWord(String string) {
        String str = string;
        int index = string.indexOf(",");
        if (-1 != index) {
            str = string.substring(0, index);
        }
        return str;
    }

    /**
     * 网络是否可用
     *
     * @param context 上下文
     * @return boolean
     */
    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (null == connectivity) {
        } else {
            NetworkInfo[] infos = connectivity.getAllNetworkInfo();
            if (infos != null) {
                for (int i = 0; i < infos.length; i++) {
                    if (infos[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * wifi是否可用
     *
     * @param context 上下文
     * @return wifi是否可用
     */
    public static boolean isWifiEnabled(Context context) {
        ConnectivityManager mgrConn = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        TelephonyManager mgrTel = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        return ((mgrConn.getActiveNetworkInfo() != null && mgrConn
                .getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED) || mgrTel
                .getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS);
    }

    public static boolean isWifi(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetInfo != null
                && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            return true;
        }
        return false;
    }

    /**
     * 3G
     *
     * @param context 上下文
     * @return boolean
     */
    public static boolean is3G(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetInfo != null
                && activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
            return true;
        }
        return false;
    }

    /**
     * MD5加密
     *
     * @param str 需加密的字符串
     * @return String
     */
    public static String MD5(String str) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
        char[] charArray = str.toCharArray();
        byte[] byteArray = new byte[charArray.length];
        for (int i = 0; i < charArray.length; i++) {
            byteArray[i] = (byte) charArray[i];
        }
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }

    /**
     * 字符串是否为空
     *
     * @param str String
     * @return boolean
     */
    public static boolean isStringEmpty(String str) {
        boolean isEmpty = false;
        if (null == str || str.trim().equals("") || "null".equals(str)) {
            isEmpty = true;
        }
        return isEmpty;
    }
}
